package it.unibz.ti2.internetPizzeria.servlets;

import it.unibz.ti2.internetPizzeria.dao.OrderDAO;
import it.unibz.ti2.internetPizzeria.dao.PropertyDAO;
import it.unibz.ti2.internetPizzeria.entities.Order;
import it.unibz.ti2.internetPizzeria.entities.Property;
import it.unibz.ti2.internetPizzeria.utils.OrderUtil;
import it.unibz.ti2.internetPizzeria.utils.ServletUtil;
import it.unibz.ti2.internetPizzeria.webAttributes.OrderInfo;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Listing orders ready to delivery
 */
public class OrdersReadyToDeliver extends HttpServlet{

	private static final long serialVersionUID = -647408377383398736L;

	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		this.doPost(arg0, arg1);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		OrderDAO orderDAO = null;
		try {
			orderDAO = new OrderDAO();			
			orderDAO.openSession();
			PropertyDAO propertyDAO = new PropertyDAO();
			propertyDAO.openSession(orderDAO);
			List<Order> orders = orderDAO.getOrders(Order.StatusReadyToDeliver);
			List<OrderInfo> orderInfos = OrderUtil.getOrderInfos(orders);
			request.setAttribute("orderInfos", orderInfos);
			request.setAttribute("pizzeriaAddress", propertyDAO.getProperty(Property.PizzeriaAddressKey).getValue());
			request.setAttribute("pizzeriaAddressLat", propertyDAO.getProperty(Property.PizzeriaAddressLatKey).getValue());
			request.setAttribute("pizzeriaAddressLng", propertyDAO.getProperty(Property.PizzeriaAddressLngKey).getValue());
			ServletUtil.redirect(request, response, "/ordersReadyToDeliver.jsp");				
		} catch (Exception ex) {			
			ex.printStackTrace();
		} finally {
			if (orderDAO != null){
				orderDAO.closeSession();
			}
		}
	}
}
